package org.uwrfidlab.nfc;

import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	// NFC parts
	private static NfcAdapter mAdapter;
	private static PendingIntent mPendingIntent;
	private static IntentFilter[] mFilters;
	private static String[][] mTechLists;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
mAdapter = NfcAdapter.getDefaultAdapter(this);
		
		// Create a generic PendingIntent that will be deliver to this activity.
		// The NFC stack
		// will fill in the intent with the details of the discovered tag before
		// delivering to
		// this activity.
		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
				getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
		// Setup an intent filter for all MIME based dispatches
		IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

		try {
			ndef.addDataType("*/*");
		} catch (MalformedMimeTypeException e) {
			throw new RuntimeException("fail", e);
		}
		//mFilters = new IntentFilter[] { ndef, };
		mFilters = null;
		// Setup a tech list for all NfcF tags
//		mTechLists = new String[][] { 
//				new String[] { MifareUltralight.class.getName() },
//				new String[] { MifareClassic.class.getName()}};
		
		mTechLists =null;

		Intent intent = getIntent();
		resolveIntent(intent);
		
    }

    private void resolveIntent(Intent intent) {
    	String action = intent.getAction();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
        		|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
        		|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
        
        	//Intent i = new Intent();
        	intent.setComponent( new ComponentName(getPackageName(), getPackageName()+".TagViewer"));
        	startActivity(intent);
        }
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_settings:
	            //open settings
	            return true;
	        case R.id.menu_about:
	        	showAboutBox();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	private void showAboutBox() {
		Intent intent = new Intent();
		intent.setComponent( new ComponentName(getPackageName(), getPackageName()+".About"));
    	startActivity(intent);	
	}

	@Override
	public void onResume() {
		super.onResume();
		 if (mAdapter != null) {
	            if (!mAdapter.isEnabled()) {
	                //showAlert(R.string.error, R.string.nfc_disabled);
	            }
			mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
					mTechLists);
		}
	}

	@Override
	public void onNewIntent(Intent intent) {
		Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
		setIntent(intent);
		resolveIntent(intent);
	}

	@Override
	public void onPause() {
		super.onPause();
		mAdapter.disableForegroundDispatch(this);
	}
}
